<?php

namespace App\Http\Repositories\Relation;

use App\Http\Repositories\Repository;
use App\Models\UserFavority;

/**
 * 用户收藏关联
 */
class UserFavoriteRepo extends Repository
{
    const MODEL = UserFavority::class;


    public function search(array $params, int $type)
    {
        $query = $this->query()
            ->where("type", $type)
            ->where("user_id", $params["user_id"]);

        if ($type == config("project.user_favorities_type.service")) {
            $query->with(['services' => function ($q) {
                $q->with(['resources', 'types', "provinces", "citys", "areas"]);
            }]);
        } else {
            $query->with(['demands' => function ($q) {
                $q->with(['resources', 'types', "provinces", "citys", "areas"]);
            }]);
        }

        // if (!empty($params['title'])) {
        // $query->where('title', 'like', '%' . $params['title'] . '%');
        // }
        // if (!empty($params['phone'])) {
        // $query->where('phone', 'like', '%' . $params['phone'] . '%');
        // }

        // if (!empty($params['introduction'])) {
        // $query->where('introduction', 'like', '%' . $params['introduction'] . '%');
        // }

        // if (!empty($params['sn'])) {
        // $query->where('sn', 'like', '%' . $params['sn'] . '%');
        // }

        // if (!empty($params['negotiate'])) {
        // $query->where('negotiate', $params['negotiate']);
        // }

        // if (!empty($params['province_id'])) {
        // $query->where('province_id', $params['province_id']);
        // }

        // if (!empty($params['city_id'])) {
        // $query->where('city_id', $params['city_id']);
        // }

        // if (!empty($params['area_id'])) {
        // $query->where('area_id', $params['area_id']);
        // }

        // if (!empty($params['type_ids'])) {
        // $object_ids = app(TypeObjectRepo::class)->getObjectIdByTypeIds($params['type_ids']);
        // $query->whereIn('id', $object_ids);
        // }

        // if (!empty($params['begin_price'])) {
        // $query->where('price', '>=', $params['begin_price']);
        // }

        // if (!empty($params['end_price'])) {
        // $query->where('price', '<=', $params['end_price']);
        // }

        // if (!empty($params['status'])) {
        // $query->where('status', $params['status']);
        // }

        // if (!empty($params['begin_time'])) {
        // $query->where('created_at', '>=', $params['begin_time']);
        // }

        // if (!empty($params['end_time'])) {
        // $query->where('created_at', '<=', $params['end_time']);
        // }


        // if (!empty($params['begin_deadline'])) {
        // $query->where('deadline', '>=', $params['begin_deadline']);
        // }

        // if (!empty($params['end_deadline'])) {
        // $query->where('deadline', '<=', $params['end_deadline']);
        // }

        return $this->paginate($query, $params["paginate"] ?? true, $params["page_num"] ?? 10);
    }

    public function favoriteOrNot(array $params): int
    {
        $favorite = $this->query()
            ->where("user_id", $params["user_id"])
            ->where("object_id", $params["object_id"])
            ->where("type", $params["type"])
            ->first();
        if (empty($favorite)) {
            parent::createOne($params);
            return 1;
        }
        $favorite->delete();
        return 0;
    }
}
